#include "client.h"


int Register(int peerFd) {
    //询问用户选择注册还是登录，以此设置标志位，并发送给服务器端
    printf("log in or register (input 1 for log in, 0 to create a new account): ");
    int flag;
    scanf("%d", &flag);
    Sendn(peerFd, &flag, sizeof(int));
    //客户端本地判断用户要求注册还是登录
    Train_t train;
    if(flag == 0) {
        //如果选择注册则循环提示用户输入新用户的用户名，并发送给服务器端
        printf("now create your account\n");
        do{
            printf("please input your username: \n");
            memset(&train, 0, sizeof(train));
            read(STDIN_FILENO, train.buf, sizeof(train.buf));
            //由于read会多读入一个换行符，此处将其去除
            for(int i = 0; i < (int)strlen(train.buf); i++) {
                if(train.buf[i] == '\n') {
                    train.buf[i] = 0;
                }
            }
            train.lenth = strlen(train.buf);
            //将用户名发送给服务器端
            Sendn(peerFd, &train, train.lenth + 4);
            //接收服务器返回的标志位，判断用户名是否已经存在，若已经存在则重新输入
            int ret = recv(peerFd, &flag, sizeof(flag), MSG_WAITALL);
            if(ret < 0) {
                perror("recv");
                return -1;
            } else if(ret == 0) {
                return -1;
            }
            if(flag == 1) {
                printf("the username you input has been registered\n");
            }
        } while(flag == 1);
        //若该用户名不存在，则提示用户输入密码，并将其发送给服务器端
        printf("please input your password: \n");
        system("stty -echo");
        read(STDIN_FILENO, train.buf, sizeof(train.buf));
        //由于read会多读入一个换行符，此处将其去除
        for(int i = 0; i < (int)strlen(train.buf); i++) {
            if(train.buf[i] == '\n') {
                train.buf[i] = 0;
            }
        }
        train.lenth = strlen(train.buf);
        //将密码发送给服务器端
        Sendn(peerFd, &train, train.lenth + 4);
    } else {
        do{
            printf("please input your username: \n");
            memset(&train, 0, sizeof(train));
            read(STDIN_FILENO, train.buf, sizeof(train.buf));
            //由于read会多读入一个换行符，此处将其去除
            for(int i = 0; i < (int)strlen(train.buf); i++) {
                if(train.buf[i] == '\n') {
                    train.buf[i] = 0;
                }
            }
            train.lenth = strlen(train.buf);
            //将用户名发送给服务器端
            Sendn(peerFd, &train, train.lenth + 4);
            //接收服务器返回的标志位，判断用户名是否已经存在，若不存在则重新输入
            int ret = recv(peerFd, &flag, sizeof(flag), MSG_WAITALL);
            if(ret < 0) {
                perror("recv");
                return -1;
            } else if(ret == 0) {
                return -1;
            }
            if(flag == 0) {
                printf("the username you input is not found\n");
            }
        } while(flag == 0);
        do{
            //若该用户名存在，则提示用户输入密码，并将其发送给服务器端
            printf("please input your password: \n");
            system("stty -echo");
            read(STDIN_FILENO, train.buf, sizeof(train.buf));
            //由于read会多读入一个换行符，此处将其去除
            for(int i = 0; i < (int)strlen(train.buf); i++) {
                if(train.buf[i] == '\n') {
                    train.buf[i] = 0;
                }
            }
            train.lenth = strlen(train.buf);
            //将密码发送给服务器端
            Sendn(peerFd, &train, train.lenth + 4);
            //从服务器接收标志位，判断密码输入是否正确，错误则提示再次输入
            int ret = recv(peerFd, &flag, sizeof(flag), MSG_WAITALL);
            if(ret < 0) {
                perror("recv");
                return -1;
            } else if(ret == 0) {
                return -1;
            }
            if(flag == 1) {
                printf("wrong password\n");
            }
        } while(flag == 1);
    }
    return 0;
}








































